
var BroadcastView = View.extend(BroadcastView = {
    init: function() {
        this.configureUI();
        this.configureSendBroadcast();

        this.loadBroadcastHistory();
    },
    
    configureSendBroadcast: function() {
        var self = this;

        $("#send_broadcast_btn").click(function( e ) {
            e.preventDefault();

            var broadcast_list_id = null, category_id = null,
                list_val = $("#send_broadcast_form #broadcast_list_id").val(),
                schedule = $("#send_broadcast_form #schedule").val(),
                message = $("#send_broadcast_form #send_broadcast_message").val(),
                category_id = null;
            
            if( list_val.indexOf('cat') > -1 ) {
                category_id = list_val.split('_')[1];
            }
            
            else if( list_val.indexOf('list') > -1 ) {
                broadcast_list_id = list_val.split('_')[1];
            }

            if ( ['','-1'].indexOf(broadcast_list_id) != -1  ) {
                return false;
            }

            if ( message.length == 0 ) {
                alert('Please enter a message that you would like to send');
                return;
            }

            confirm('You are about to schedule an SMS Broadcast to the selected list. Do you want to continue?', function() {

                function enableFields( enable ) {
                    if( enable == true ) {
                        $("#send_broadcast_form input").removeAttr('disabled');
                    } else {
                        $("#send_broadcast_form input").attr('disabled', true);
                    }
                }

                enableFields(false);

                var url = $("#send_broadcast_form").attr('action');
                var params = {
                    schedule: schedule,
                    message: message
                };
                
                if( broadcast_list_id ) {
                    params['broadcast_list_id'] = broadcast_list_id;
                }
                
                if( category_id ) {
                    params['category_id'] = category_id;
                }
                

                $.ajax({
                    url: url,
                    data: params,
                    type: 'post',
                    dataType: 'json',
                    success: function( response ) {

                        if ( response.status == 'success' ) {
                            $('#sms_account_balance').text(response.account_balance);
                            $("#send_broadcast_form textarea").val('');
                            $("#send_broadcast_form .pillbox li").remove();

                            self.loadBroadcastHistory();
                            
                            alert(response.message);
                        }
                        
                        else {
                            alert( response.message );
                        }

                        enableFields(true);
                    },
                    error: function( xhr ) {
                        console.log(xhr.responseText);
                        enableFields(true);
                        alert("An error whiles trying to send this message. Please refresh and try again.");
                    }
                });
            });
        });
    },
    configureUI: function() {
        $("#broadcast_list_id").select2();
        
        $(".schedule_opt").change(function() {
            if( $(this).is(":checked") && $(this).val() == "schedule" ) {
                $("#schedule_row").show('fast');
                $("#send_broadcast_btn").html('<i class="fa fa-clock-o"></i> Schedule Broadcast');
            }
            else {
                $("#schedule_row").hide('fast');
                $("#send_broadcast_btn").html('<i class="fa fa-envelope"></i> Send Broadcast');
            }
        });
    },
    
    loadBroadcastHistory: function() {

        var self = this;
        var url = $("#broadcast_history_url").val();

        $("#broadcast-history").html("Loading ...");

        $.ajax({
            url: url,
            type: 'post',
            success: function( response ) {
                self.loaded = true;

                $("#broadcast-history").html(response);
            },
            error: function( xhr ) {
                console.log(xhr.responseText);
            }
        });
    }
});

$(document).ready(function() {
    var messagesView = new BroadcastView();
});
